/**
 * 缓存用户信息
 */
import { defineStore } from 'pinia'
import Cache from '@/global/Cache'
import * as CacheKey from '@/global/CacheKey'

export default defineStore('currentUsers', {
  state: () => {
    return {
      currentUsers: Cache.get(CacheKey.CURRENT_USER, null) as any
    };
  },
  actions: {
    loadState() {
      const currentState = Cache.get(CacheKey.CURRENT_USER, null)
      this.$patch({ currentUsers: JSON.parse(currentState) });
    },
    saveState(newState: any) {
      this.currentUsers = newState;
      Cache.save(CacheKey.CURRENT_USER, this.currentUsers);
      // this.$patch({ currentUsers: newState });
    },
    getState(): any {
      return Cache.get(CacheKey.CURRENT_USER, {});
    },
    resetState() {
      this.$reset();
    },
    clearState() {
      Cache.remove(CacheKey.CURRENT_USER);
    }
  }
})